const OrderModel = require('../models/OrderModel')
const CarModel = require('../models/CarModel')

const shopService = {
  async addOrder(data) {
    await CarModel.updateOne(
      {
        _id: data.car,
      },
      {
        $set: {
          is_rend: 1,
        },
      }
    ).exec()
    return OrderModel.create(data)
  },
  getUserOrder(user) {
    return OrderModel.find({
      user,
    })
      .sort('-createdAt')
      .populate('user', { full_name: 1, phone: 1 })
      .populate('car', { license_number: 1, is_rend: 1 })
      .populate('shop', { address: 1, shopName: 1, tel: 1 })
  },
  getOrder() {
    return OrderModel.find()
      .sort('-createdAt')
      .populate('user', { full_name: 1, phone: 1 })
      .populate('car', { license_number: 1, is_rend: 1 })
      .populate('shop', { address: 1, shopName: 1, tel: 1 })
  },
  updateOrder(_id) {
    return CarModel.updateOne(
      {
        _id,
      },
      {
        $set: {
          is_rend: 0,
        },
      }
    )
  },
  deleteOrder(_id) {
    return OrderModel.deleteOne({
      _id,
    })
  },
}

module.exports = shopService
